<?php

class BaseView {

    private $_controlador;
    private $params;
    private $css_tags;

    public function __construct(Request $peticion) {
        $this->_controlador = $peticion->getController();
        $this->params = new ParamsTransfer;
        $this->css_tags = array();
    }

    public function set_value($attribute_name, $value) {
        $this->params->set_value($attribute_name, $value);
    }

    public function display($view) {
        $this->params->set_value('controller_name', $this->_controlador);
        $this->params->set_value('css_tags', $this->css_tags());
        $this->params->set_value('js_tags', $this->js_tags());
        $this->params->display($this->content_path($view));
    }

    private function css_tags() {
        $str_css_tags = '';
        /* $css_directory_paths = array(
          VIEW_PATH . 'layout' . DS . DEFAULT_LAYOUT . DS . 'css' . DS . '*.css',
          VIEW_PATH . $this->_controlador . DS . 'css' . DS . '*.css',
          ROOT . 'public' . DS . 'css' . DS . '*.css');
         */
//        foreach ($css_directory_paths as $directory) {
        if ($this->css_tags) {
            foreach ($this->css_tags as $tag) {
                $replaced_tag = str_replace(ROOT, BASE_URL, $tag);
                $str_css_tags .= $replaced_tag; //'<link href="' . $ref . '" rel="stylesheet" type="text/css" />';
            }
        }
//        }
        return $str_css_tags;
    }

    private function js_tags() {
        $str_js_tags = '';
        $js_directory_paths = array(
            VIEW_PATH . 'layout' . DS . DEFAULT_LAYOUT . DS . 'js' . DS . '*.js',
            VIEW_PATH . $this->_controlador . DS . 'js' . DS . '*.js',
            ROOT . 'public' . DS . 'js' . DS . '*.js');

        foreach ($js_directory_paths as $directory) {
            foreach (glob($directory) as $filename) {
                $ref = str_replace(ROOT, BASE_URL, $filename);
                $str_js_tags .= '<script src="' . $ref . '"></script>';
            }
        }
        return $str_js_tags;
    }

    private function content_path($view) {
        if(strstr($view, DS)):
            $content_path = VIEW_PATH . $view;
        else:
            $content_path = VIEW_PATH . $this->_controlador . DS . $view;
        endif;


        if (is_readable($content_path . '.phtml')) {
            $content_path = $content_path . '.phtml';
        } elseif (is_readable($content_path . '.php')) {
            $content_path = $content_path . '.php';
        } else {
            throw new Exception('Error de vista');
        }

        return $content_path;
    }

    function add_css_tags($ref, $html_options = array()) {
        $tag = '<link href="' . $ref . '" rel="stylesheet" type="text/css" ' . $this->html_attributes($html_options) . '/>';
        array_push($this->css_tags, $tag);
    }

    function html_attributes($attributes) {
        $html_attributes = '';
        if (sizeof($attributes != 0)) {
            foreach ($attributes as $attr_name => $attr_value):
                $html_attributes .= "$attr_name=\"$attr_value\" ";
            endforeach;
        }
        return $html_attributes;
    }

}

?>
